/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import com.xy.biz.rs.client.RecordTemplateClient;
import com.xy.biz.rs.domain.converter.RecordTemplateConverter;
import com.xy.biz.rs.domain.converter.RecordTemplateGroupConverter;
import com.xy.biz.rs.domain.converter.RecordTemplateItemConverter;
import com.xy.biz.rs.domain.query.RecordTemplateByIdQuery;
import com.xy.biz.rs.domain.query.RecordTemplateQuery;
import com.xy.biz.rs.domain.req.*;
import com.xy.biz.rs.domain.resp.GetRecordTemplateGroupResponse;
import com.xy.biz.rs.domain.resp.GetRecordTemplateResponse;
import com.xy.biz.rs.service.RecordTemplateService;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.enums.ApplyStatusEnum;
import com.xy.lang.enums.AuditStatusEnum;
import com.xy.lang.util.EnumUtils;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.*;

/**
 *
 *
 * @author zsp
 * @date 2022-1-7
 */
@Api(tags = "记录模板服务")
@Slf4j
@RestController
@RequestMapping("/api/rs/record_template")
public class RecordTemplateController implements RecordTemplateClient {

    @Autowired
    private RecordTemplateService recordTemplateService;

    @ApiOperation("保存记录模板")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveRecordTemplate(@RequestBody SaveRecordTemplateRequest request) {
        return recordTemplateService.saveRecordTemplate(RecordTemplateConverter.toBO(request));
    }

    @ApiOperation("更新记录模板")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateRecordTemplate(@RequestBody SaveRecordTemplateRequest request) {
        return recordTemplateService.updateRecordTemplate(RecordTemplateConverter.toBO(request));
    }

    @ApiOperation("删除记录模板")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteRecordTemplate(@RequestBody GetRecordTemplateRequest request) {
        return recordTemplateService.deleteRecordTemplate(request.getRecordTemplateId());
    }

    @ApiOperation("获取记录模板")
    @Validatable
    @PostMapping(value = "/get")
    public Result<GetRecordTemplateResponse> getRecordTemplate(@RequestBody GetRecordTemplateRequest request) {
        return Result.success(RecordTemplateConverter.toResponse(recordTemplateService.getRecordTemplate(RecordTemplateByIdQuery.builder()
                .recordTemplateId(request.getRecordTemplateId())
                .withGroup(request.getWithGroup())
                .withItem(request.getWithItem())
                .build())));
    }

    @ApiOperation("分页查询记录模板")
    @PostMapping(value = "/page")
    public Result<Page<GetRecordTemplateResponse>> pageRecordTemplate(@RequestBody ListRecordTemplateRequest request) {
        RecordTemplateQuery query = RecordTemplateConverter.toQuery(request);
        List<GetRecordTemplateResponse> list = RecordTemplateConverter.toResponse(recordTemplateService.listRecordTemplate(query));
        int count = recordTemplateService.countRecordTemplate(query);
        return Result.success(new Page<>(list, count));
    }

    @ApiOperation("查询记录模板")
    @PostMapping(value = "/list")
    public Result<List<GetRecordTemplateResponse>> listRecordTemplate(@RequestBody ListRecordTemplateRequest request) {
        return Result.success(RecordTemplateConverter.toResponse(recordTemplateService.listRecordTemplate(RecordTemplateConverter.toQuery(request))));
    }

    @ApiOperation("更新记录模板状态")
    @Validatable
    @PostMapping(value = "/update_status")
    public Result<?> updateRecordTemplateForStatus(@RequestBody UpdateRecordTemplateStatusRequest request) {
        return recordTemplateService.updateRecordTemplateForStatus(request);
    }

    @ApiOperation("保存记录模板组")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_group")
    public Result<?> saveRecordTemplateGroup(@RequestBody SaveRecordTemplateGroupRequest request) {
        return recordTemplateService.saveRecordTemplateGroup(RecordTemplateGroupConverter.toBO(request));
    }

    @ApiOperation("更新记录模板组")
    @Validatable
    @PostMapping(value = "/update_group")
    public Result<?> updateRecordTemplateGroup(@RequestBody UpdateRecordTemplateGroupRequest request) {
        return recordTemplateService.updateRecordTemplateGroup(RecordTemplateGroupConverter.toBO(request));
    }

    @ApiOperation("删除记录模板组")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/delete_group")
    public Result<?> deleteRecordTemplateGroup(@RequestBody GetRecordTemplateGroupRequest request) {
        return recordTemplateService.deleteRecordTemplateGroup(request.getRecordTemplateGroupId());
    }

    @ApiOperation("列表查询记录模板组")
    @Override
    @PostMapping(value = "/list_group")
    public Result<List<GetRecordTemplateGroupResponse>> listRecordTemplateGroupByTemplateId(@RequestBody GetRecordTemplateRequest request) {
        return Result.success(
                RecordTemplateGroupConverter.toResponse(recordTemplateService.listRecordTemplateGroupByTemplateId(
                        request.getRecordTemplateId(),
                        Objects.equals(true, request.getWithItem()))));
    }

    @Override
    public Result<Map<Long, List<GetRecordTemplateGroupResponse>>> groupRecordTemplateGroupByTemplateId(GroupRecordTemplateRequest request) {
        return Result.success(
                recordTemplateService.groupRecordTemplateGroupByTemplateId(
                        request.getRecordTemplateIdList(),
                        Objects.equals(true, request.getWithItem())));
    }

    @Override
    public Result<List<GetRecordTemplateResponse>> listRecordTemplateId(Collection<Long> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return Result.success(Collections.emptyList());
        }
        return Result.success(
                RecordTemplateConverter.toResponse(
                        recordTemplateService.listRecordTemplate(RecordTemplateQuery.max()
                                .recordTemplateIdList(new ArrayList<>(collection)).build())));
    }

    @ApiOperation("保存记录模板项")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_item")
    public Result<?> saveRecordTemplateItem(@RequestBody SaveRecordTemplateItemRequest request) {
        return recordTemplateService.saveRecordTemplateItem(RecordTemplateItemConverter.toBO(request));
    }

    @ApiOperation("复制模板")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/copy")
    public Result<?> copyRecordTemplate(@RequestBody GetRecordTemplateRequest request) {
        return recordTemplateService.copyRecordTemplate(request);
    }

    @ApiOperation("复制模板组")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/copy_group")
    public Result<?> copyRecordTemplateGroup(@RequestBody GetRecordTemplateGroupRequest request) {
        return recordTemplateService.copyRecordTemplateGroup(request);
    }

    @ApiOperation("保存记录模板项图片")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_item_picture")
    public Result<?> saveRecordTemplateItemPicture(MultipartHttpServletRequest request) {
        return recordTemplateService.saveRecordTemplateItemPicture(request);
    }

    @ApiOperation("更新记录模板项")
    @Validatable
    @PostMapping(value = "/update_item")
    public Result<?> updateRecordTemplateItem(@RequestBody UpdateRecordTemplateItemRequest request) {
        return recordTemplateService.updateRecordTemplateItem(RecordTemplateItemConverter.toBO(request));
    }

    @ApiOperation("删除记录模板项")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/delete_item")
    public Result<?> deleteRecordTemplateItem(@RequestBody GetRecordTemplateItemRequest request) {
        return recordTemplateService.deleteRecordTemplateItem(request.getRecordTemplateItemId());
    }

    @ApiOperation("更新排序号")
    @Validatable
    @PostMapping(value = "/update_sequence")
    public Result<?> updateRecordTemplateSequence(@RequestBody UpdateRecordTemplateSequenceRequest request) {
        return recordTemplateService.updateRecordTemplateSequence(request);
    }

    @ApiOperation("查询选项")
    @GetMapping(value = "/list_pre_options")
    public Result<Map<String, List>> listPreOptions() {
        Map<String, List> map = new HashMap<>();
        map.put("status", EnumUtils.toKeyValueList(ApplyStatusEnum.values(),
                (e) -> ((ApplyStatusEnum) e).getName()));
        map.put("auditStatus", EnumUtils.toKeyValueList(AuditStatusEnum.values(),
                (e) -> ((AuditStatusEnum) e).getName()));
        return Result.success(map);
    }

}
